package dao

import (
	"go_server/game_server/database/mysql"
	"go_server/game_server/util"
	"go_server/zinx/zconf"
	"go_server/zinx/zlog"
	"gorm.io/gorm"
)

type Role struct {
	RoleId int64 `gorm:"column:Id;primary"`
	Uid string `gorm:"column:uid"`
	Platform string `gorm:"column:platform"`
	Channel string `gorm:"column:channel"`
	Sid int `gorm:"column:sid"`
	Name string `gorm:"column:name"`
}

func (Role) TableName() string {
	return "role"
}

func NewRole(uid string, roleId int64)  {
	role := &Role{
		RoleId: roleId,
		Uid: uid,
		Platform: "",
		Channel: "",
		Sid: zconf.GlobalObject.Id,
		Name: "",
	}
	db := mysql.GetServerDBConnection()
	if err := db.Create(role).Error; err != nil {
		zlog.Errorf("create Role %v data failed: %v", role, err)
	}
}

var(
	all_role_field = util.GetGormFields(Role{})
)

func GetRoleByRoleId(roleId int64) *Role {
	db := mysql.GetServerDBConnection()
	var role Role
	if err := db.Select(all_role_field).Where("Id=?", roleId).First(&role).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			zlog.Errorf("GetRoleByRoleId ERR!!! Id: %d, err: %v", roleId, err)
		}
		return nil
	}
	return &role
}
